package com.xxxt.jxj.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxt.dao.StudentMapper;
import com.xxxt.dao.TeacherMapper;
import com.xxxt.entity.ClassGrade;
import com.xxxt.entity.Grade;
import com.xxxt.entity.Student;
import com.xxxt.entity.Teacher;
import com.xxxt.jxj.service.ClassGradeService;
import com.xxxt.jxj.service.GradeService;
import com.xxxt.utils.UUIDUtil;


@Controller
@RequestMapping("/classGrade")
public class ClassGradeController {

	@Resource
	private ClassGradeService classGradeService;
	@Resource
	private TeacherMapper teacherMapper;
	@Resource
	private GradeService gradeService;
	@Resource
	private StudentMapper studentMapper;
	/**
	 * 修改班级(普通使用:不能修改ID)
	 * @param ClassGradeId
	 * @param classGradeName
	 * @param tno
	 * @param gradeId
	 * @return
	 */
	@RequestMapping(value="/updateClassGrade",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject updateClassGrade(HttpSession session,String classGradeName,String tno,String gradeId){
		JSONObject json = new JSONObject();

		try {
			ClassGrade classGrade1=(ClassGrade) session.getAttribute("queryOneClassGradeByClassGradeId");
			
			Teacher teacher = teacherMapper.queryOneTeacherByTno(tno);
			if (teacher==null) {
				json.put("status", -4);
				json.put("message", "抱歉，当前列表中没有该老师！");
				return json;
			}
			Grade grade = gradeService.selcetOneGradeByGradeId(gradeId);
			if (grade==null) {
				json.put("status", -3);
				json.put("message", "抱歉，当前列表中没有该年级！");
				return json;
			}
			
				List<ClassGrade> list = classGradeService.selectAllClassGradeByGradeId(gradeId);//遍历若有相同不给修改
				for (ClassGrade classGrade2 : list) {
					if (classGrade2.getClassGrade()==classGradeName||classGrade2.getClassGrade().equals(classGradeName)) {
						json.put("status", -2);
						json.put("message", "当前年级已有相同的班级名称，请不要修改为相同的");
						return json;
					}
				}
				classGrade1.setClassGrade(classGradeName);//班级名称
				classGrade1.setGrade(grade);
				boolean updateClassGrade = classGradeService.updateClassGrade(classGrade1);
				if (updateClassGrade) {
					json.put("status", 0);
					json.put("message", "修改成功");
				}
				else {
					json.put("status", -1);
					json.put("message", "修改失败");
				}
		} catch (Exception e) {			
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "修改失败");
			return json;	
		}
		
						
		return json;		
	}
	
	/***
	 * 插入班级
	 * @param schoolId
	 * @param collegeId
	 * @param collegeName
	 * @return
	 */
	@RequestMapping(value="/insertOneClassGrade",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject insertOneClassGrade(String gradeId,String classGradeName){
		JSONObject json = new JSONObject();
		try {
			Grade grade = gradeService.selcetOneGradeByGradeId(gradeId);
			if (grade==null) {
				json.put("status", 2);
				json.put("message", "当前年级不存在,添加失败");
				return json;
			}	
			List<ClassGrade> list = classGradeService.selectAllClassGradeByGradeId(gradeId);
			ClassGrade classGrade = new ClassGrade();
			classGrade.setClassGradeId(UUIDUtil.getUUID());
			classGrade.setClassGrade(classGradeName);
			classGrade.setGrade(grade);
			for (ClassGrade classGrade2 : list) {						
				if (classGrade2.getClassGrade()==classGradeName||classGrade2.getClassGrade().equals(classGradeName)) {
					json.put("status", -2);
					json.put("message", "当前年级已有相同的班级名称存在，请不要重复添加！");
					return json;			
				}
			}			
			boolean insertOneClassGrade = classGradeService.insertOneClassGrade(classGrade);
			if (insertOneClassGrade) {
				json.put("status", 0);
				json.put("message", "添加成功");
			}
			else {
				json.put("status", 1);
				json.put("message", "添加失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "添加失败");
			return json;	
		}		
		return json;		
	}
	/**'
	 * 查询单个班级信息
	 * @param collegeId
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/queryOneClassGradeByClassGradeId",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject queryOneClassGradeByClassGradeId(String ClassGradeId,HttpSession session){
		JSONObject json = new JSONObject();
		try {
			ClassGrade queryOneClassGradeByClassGradeId = classGradeService.queryOneClassGradeByClassGradeId(ClassGradeId);
			if (queryOneClassGradeByClassGradeId!=null) {
				session.setAttribute("queryOneClassGradeByClassGradeId", queryOneClassGradeByClassGradeId);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("classGrade", queryOneClassGradeByClassGradeId);			
			}else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询成功");
			return json;	
		}	
		return json;				
	}
	/**
	 * 修改单个班级信息（管理员,可修改ID）
	 * @param collegeId
	 * @param schoolId
	 * @param collegeName
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/updateClassGradeForAdmin",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject updateClassGradeForAdmin(String gradeId,String classGradeId,String classGradeName,HttpSession session){
		JSONObject json = new JSONObject();
		try {
			ClassGrade classGrade=(ClassGrade) session.getAttribute("queryOneClassGradeByClassGradeId");
			
			List<Student> queryOneStudentByClassGrade = studentMapper.queryStudentByClassGrade(classGrade.getClassGradeId());
			if (queryOneStudentByClassGrade.size()>0 & !classGrade.getClassGradeId().equals(classGradeId) ) {
				json.put("status", -6);
				json.put("message", "抱歉，当前ID已有关联不能更改ID！");
				return json;
			}	
			Grade grade = gradeService.selcetOneGradeByGradeId(gradeId);
			if (grade==null) {
				json.put("status", -1);
				json.put("message", "抱歉，当前列表中没有该年级！");
				return json;
			}
			List<ClassGrade> selectAllClassGradeByGradeId = classGradeService.selectAllClassGradeByGradeId(gradeId);

			for (ClassGrade classGrade2 : selectAllClassGradeByGradeId) {			
				if (classGrade2.getClassGradeId()==classGradeId||classGrade2.getClassGradeId().equals(classGradeId)) {
					json.put("status", -2);
					json.put("message", "当前年级已有相同的班级ID存在，请不要重复添加！");
					return json;
				}	
				if (classGrade2.getClassGrade()==classGradeName||classGrade2.getClassGrade().equals(classGradeName)) {
					json.put("status", -3);
					json.put("message", "当前年级已有相同的班级名称存在，请不要重复添加！");
					return json;			
				}
			}
			List<ClassGrade> selcetAllSpecialty = classGradeService.selcetAllClassGrade();
			for (ClassGrade classGrade3 : selcetAllSpecialty) {			
				if (classGrade3.getClassGradeId()==classGradeId||classGrade3.getClassGradeId().equals(classGradeId)) {
					json.put("status", -4);
					json.put("message", "已有相同的班级ID存在，请不要重复添加！");
					return json;
				}								
			}
			boolean updateClassGradeForAdmin = classGradeService.updateClassGradeForAdmin(classGradeId, classGradeName, grade.getGradeId(), classGrade.getClassGradeId());
			if (updateClassGradeForAdmin) {
				json.put("status", 0);
				json.put("message", "修改成功");
			}
			else {
				json.put("status", 1);
				json.put("message", "修改失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "修改失败");
			return json;	
		}		
		return json;
		}	
	/**
	 * 删除单个班级信息
	 * @param collegeId
	 * @return
	 */
	@RequestMapping(value="/deleteClassGrade",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject deleteSpecialty(String classGradeId){
		JSONObject json = new JSONObject();
		try {
			boolean deleteClassGrade = classGradeService.deleteClassGrade(classGradeId);
			if (deleteClassGrade) {
				json.put("status", 0);
				json.put("message", "删除成功");
			}
			else {
				json.put("status", -1);
				json.put("message", "删除失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "删除失败");
			return json;	
		}		
		return json;
	}
	/**
	 * 根据年级id 分页查询班级信息
	 * @param schoolId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/selcetAllPageClassGradeByGradeId",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selcetAllPageClassGradeByGradeId(String gradeId,int pageNum,int pageSize){
		JSONObject json = new JSONObject();
		try {
			PageHelper.startPage(pageNum, pageSize);
			List<ClassGrade> selcetAllPageClassGradeByGradeId = classGradeService.selcetAllPageClassGradeByGradeId(gradeId, pageNum, pageSize);
			if (selcetAllPageClassGradeByGradeId.size()>0) {
				PageInfo<ClassGrade> pageInfo=new PageInfo<>(selcetAllPageClassGradeByGradeId);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("pageInfo", pageInfo);
			}
			else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
			return json;	
		}		
		return json;
		}
	/**
	 * 根据 班级id,老师id 分页查询学生信息
	 * @param schoolId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/selectStudentByTnoAndClassGrade",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selectStudentByTnoAndClassGrade(String classGradeId,String tno ,int pageNum,int pageSize){
		JSONObject json = new JSONObject();
		try {
			PageHelper.startPage(pageNum, pageSize);			
			List<Student> selectStudentByTnoAndClassGrade = classGradeService.selectStudentByTnoAndClassGrade(tno, classGradeId, pageNum, pageSize);
			if (selectStudentByTnoAndClassGrade.size()>0) {
				PageInfo<Student> pageInfo=new PageInfo<>(selectStudentByTnoAndClassGrade);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("pageInfo", pageInfo);
			}
			else if (selectStudentByTnoAndClassGrade.size()==0) {
				json.put("status", -2);
				json.put("message", "当前此选项下没有你的学生");
			}
			else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
			return json;	
		}		
		return json;
		}
}
